#include <p18F8722.h>
#include <usart.h>
#include <delays.h>
#pragma code
void delay (void)
{
	unsigned int i;
	for (i = 0; i < 10000 ; i++);
}
#pragma code
void main (void)
{
	TRISC = 0xBF;  //Poneren Output el unico bit necesario: RC6, el resto Input
	TRISD = 0x00;  //Poner en output todo el puertoD
 	PORTD = 0xFF;
     Open1USART(
		USART_TX_INT_OFF & 
		USART_RX_INT_ON & 
		USART_ASYNCH_MODE & 
		USART_EIGHT_BIT & 
		USART_CONT_RX & 
		USART_BRGH_LOW, 129);
	RCONbits.IPEN = 1; //Habilitar prioridad de interrupciones
	IPR1bits.RCIP = 1; //Hacer que la recepcion sea alta prioridad;
	INTCONbits.GIEH = 1; //Habilitar interrupciones altas
     while(1)
     {
		while(Busy1USART()){}
        putrs1USART( "." );
        Delay10KTCYx(500);
     }
}



//Lets the compiler know that this function is the ISR
#pragma interrupt high_isr
void high_isr (void)
	{
		unsigned char c = getc1USART(); //get a single character off the USART line
		PORTD = c;
		putrs1USART( "Data Received" );
			//while(Busy1USART())
			//{} //Esperar a que la Usart este disponible
			//putrs1USART( "Received Data!" );		//put a single character on the USART line
			////dloop++;
			////if (dloop > 5) dloop = 0;
			//PIR1bits.RCIF = 0; //reset the ISR flag.
	}



//Lets the compiler know the location of the ISR
#pragma code high_vector = 0x08
void interrupt_at_high_vector(void)
	{
	_asm goto high_isr _endasm
	}
